﻿using System;
using PersonaLibrary.Data;

namespace PersonaLibrary.ViewModel
{
	internal abstract class EditableItemViewModel<T> : UserControlViewModel, ICommitCommand
    {
        private RelayCommand _commitCommand;
		private RelayCommand _deleteCommand;

        public event Action Commited;
		public event Action Deleted;

        public RelayCommand CommitCommand
        {
            get
            {
                if (_commitCommand == null)
                    _commitCommand = new RelayCommand(param => Commit(), param => CanCommit());
                return _commitCommand;
            }
        }

		public RelayCommand DeleteCommand
		{
			get
			{
				if (_deleteCommand == null)
					_deleteCommand = new RelayCommand(param => Delete(), param => CanDelete());
				return _deleteCommand;
			}
		}

        public T EditableItem { protected set; get; }

        public virtual void Commit()
        {
            
            if (Commited != null) 
				Commited();
        }

        public virtual bool CanCommit()
        {
            return true;
        }

		public virtual void Delete()
		{
			if (Deleted != null)
				Deleted();
		}

		public virtual bool CanDelete()
		{
			return true;
		}
    }
}
